<?php

namespace app\admin\controller;

use app\admin\model\Nav as NavModel;
use app\BaseController;

class Nav extends BaseController
{
    /* 列表 */
    public function index()
    {
        $cid = input('cid', 0, 'intval');
        $data = NavModel::order('sort asc')->where('cid', $cid)->select();
        return show(config('status.success'), '获取列表', $data);
    }

    /* 添加 */
    public function add()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $name = input('name', '');
        $cover = input('cover', '');
        $url = input('url', '');
        $sort = input('sort', 0, 'intval');
        $status = input('status', 0, 'intval');

        $result = NavModel::create([
            'cid' => $cid,
            'name' => $name,
            'cover' => $cover,
            'url' => $url,
            'sort' => $sort,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '添加成功');
        } else {
            return show(config('status.error_insert'), '添加失败');
        }
    }

    /* 获取 */
    public function get()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $cid = input('cid', 0, 'intval');
        $data = NavModel::where(['id' => $id, 'cid' => $cid])->find();
        if ($data) {
            return show(config('status.success'), '查询成功', $data);
        } else {
            return show(config('status.error_find'), '查询失败');
        }
    }

    /* 修改 */
    public function modify()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $name = input('name', '');
        $cover = input('cover', '');
        $url = input('url', '');
        $sort = input('sort', 0, 'intval');
        $status = input('status', 0, 'intval');

        $result = NavModel::update([
            'id' => $id,
            'name' => $name,
            'cover' => $cover,
            'url' => $url,
            'sort' => $sort,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }

    /* 切换 */
    public function switch()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $status = input('status', 0, 'intval');

        $result = NavModel::update([
            'id' => $id,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '状态切换成功');
        } else {
            return show(config('status.error_switch'), '状态切换失败');
        }
    }

    /* 删除 */
    public function delete()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $result = NavModel::destroy($id);
        if ($result) {
            return show(config('status.success'), '删除成功');
        } else {
            return show(config('status.error_delete'), '删除失败');
        }
    }
}
